package com.gmu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gmu.domain.ResResult;
import com.gmu.mapper.TblActivitiesMapper;
import com.gmu.mapper.TblSampleAddressMapper;
import com.gmu.pojo.TblActivities;
import com.gmu.pojo.TblActivitiesAddress;
import com.gmu.mapper.TblActivitiesAddressMapper;
import com.gmu.pojo.TblSampleAddress;
import com.gmu.service.TblActivitiesAddressService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sun.corba.se.impl.orbutil.ObjectStreamClass_1_3_1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 活动地点记录 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-07-24
 */
@Service
public class TblActivitiesAddressServiceImpl extends ServiceImpl<TblActivitiesAddressMapper, TblActivitiesAddress> implements TblActivitiesAddressService {

    @Autowired
    private TblActivitiesAddressMapper tblActivitiesAddressMapper;

    @Autowired
    private TblActivitiesMapper tblActivitiesMapper;

    @Autowired
    private TblSampleAddressMapper tblSampleAddressMapper;

    /**
     * 采样地点设置窗口数量
     * @param activitiesId 活动id
     * @param addressId 采样地点id
     * @param addressNumber 窗口数量
     * @return
     */
    @Override
    public ResResult<?> setAddressNumber(Integer activitiesId, Integer addressId, Integer addressNumber) {
        TblActivities tblActivities = tblActivitiesMapper.selectById(activitiesId);
        if (Objects.isNull(tblActivities)){
            return new ResResult<>(404,"不存在该活动");
        }
        TblSampleAddress tblSampleAddress = tblSampleAddressMapper.selectById(addressId);
        if (Objects.isNull(tblSampleAddress)){
            return new ResResult<>(404,"不存在该地点");
        }

        for (int i = 0; i < addressNumber; i++) {
            TblActivitiesAddress tblActivitiesAddress = new TblActivitiesAddress();
            tblActivitiesAddress.setActivitiesId(activitiesId);
            tblActivitiesAddress.setSampleAddressId(addressId);
            tblActivitiesAddress.setAddressNumber( (i+1) + "号窗口");
            tblActivitiesAddressMapper.insert(tblActivitiesAddress);
        }
        return new ResResult<>(200,"设置成功");
    }

    @Override
    public ResResult<?> selectWindows(Integer activitiesId, Integer addressId) {
        HashMap<String, Integer> map = new HashMap<>();
        map.put("activities_id",activitiesId);
        map.put("sample_address_id",addressId);
        QueryWrapper<TblActivitiesAddress> wrapper = new QueryWrapper<>();
        wrapper.allEq(map);
        List<TblActivitiesAddress> tblActivitiesAddresses = tblActivitiesAddressMapper.selectList(wrapper);
        return new ResResult<>(200,tblActivitiesAddresses);
    }
}
